var api = 'http://mb3.diyuapp.com:8080/freeapp/';
angular.module('httpService', [])
    .factory("httpService", [
        "$http", "$q", "$rootScope", "$ionicLoading", "ENV",
        function($http, $q, $rootScope, $ionicLoading, ENV) {
            var httpService, methods;
            methods = ['post', 'put'];
            httpService = function(url, data, method, cache) {
                var config, q;
                q = $q.defer();
                config = {
                    method: method || "get",
                    url: api + url,
                    timeout: 60000,
                    cache: cache || false,
                    headers: {
                        "Content-Type": "application/json"
                    }
                };
                if (methods.indexOf(method) !== -1) {
                    config.data = data;
                } else {
                    config.params = data;
                }
                $http(config).success(function(data, status, headers, config) {
                    q.resolve({
                        data: data,
                        code: parseInt(status, 10)
                    });
                }).error(function(data, status, headers, config) {
                    q.reject(data);
                })["finally"](function() {
                    $rootScope.$broadcast("scroll.refreshComplete");
                    $rootScope.$broadcast("scroll.infiniteScrollComplete");
                    $ionicLoading.hide();
                });
                return q.promise;
            };
            return {
                query: httpService
            };
        }
    ]);
